<?php
namespace app\api\controller;

use think\Controller;
use think\Cache;
//use GatewayClient\Gateway;

header("Access-Control-Allow-Origin:*");

class BaseController extends Controller{
	//初始化方法
	protected function initialize(){

	 	parent::initialize();

		header("Access-Control-Allow-Origin:*");
		header("Access-Control-Allow-Methods:*");
		header("Access-Control-Allow-Headers:*");
		//清除所有缓存
		//app('cache')->clear();

		//内存缓存 内存
		ini_set ('memory_limit', '800M');
		//接口白名单

		//不用用户登录可以访问的页面
		$no_user_id_arr	= array('code','login','getdownloadurl','checkslogin','sendsmscode','getrechargeaddress',
            'register','checksmsresetpw','resetpassword','backdata','currdata',
            'repayment','signin','gettaskranklist','gettaskclasslist','getvipuserexpire',
            'taskordertrial','taskzy','upload','getnotice','getnoticedetail');

		$action = request()->action();

		//获取提交用户提交数据 并保存
		$param = input('param.');

		//更新vip会员
		$UserVipData	= model('UserVip')->where(array(['state','=',1],['etime','<',strtotime(date("Y-m-d",time()))]))->select()->toArray();

		foreach($UserVipData as $key=>$value){
			//会员过期
			$isup =  model('UserVip')->where('id' , $value['id'])->update(array('state'=>3));
			//更新会员等级
			if($isup){
				model('Users')->where('id', $value['uid'])->update(array('vip_level'=>1));//普通会员
			}
		}


		if(!in_array($action,$no_user_id_arr)){

			$user_token = isset($param['token']) && $param['token'] ? $param['token'] : '';

			$lang = isset($param['lang']) && $param['lang'] ? $param['lang'] : 'en';

            $langs = config($lang . '.lang');
            if (!is_array($langs)||count($langs)<3){
                $langs = config( 'en.lang');
            }
            if (!is_array($langs)||count($langs)<3){
                $langs = array(
                    "access denied","access denied3","access denied2",
                );
            }

			//检查 userid usertoken
			if(!$user_token){
				$data['code'] = 203;
				$data['code_dec'] = $langs[0];
				ajax_return($data);
			}

			$user_token		= stripslashes($user_token);
			$userArr 		= explode(',',auth_code($user_token,'DECODE'));//用户信息数组
			$uid 			= $userArr[0];//uid
			$username 		= $userArr[1];//uid

			//检查缓存是否存在
			if(!cache('C_token_'.$uid)){
				$data['code']		= 203;//没有登录
				$data['code_dec']	= $langs[0];
				ajax_return($data);
			}

			if(cache('C_token_'.$uid) != $user_token){
				$data['code']		= 204;//长时间没操作请重新登录
				$data['code_dec']	= $langs[1];
				ajax_return($data);
			}

			//检查用户是否存在
			$isuser = model('Users')->where(array('id'=>$uid,'username'=>$username,'state'=>1))->count();

			if(!$isuser){
				$data['code']		= 203;
				$data['code_dec']	= $langs[2];
				ajax_return($data);
			}


			//增加缓存的时间
			cache('C_token_'.$uid,$user_token,3600 * 24 * 7);

			//登录接口 添加提交数据日志
			foreach ($param as $key => $value) {
				$params[] = $key;
				$values[] = $value;
			}

			$homelog = array(
				'uid'		=>	$uid,
				'time'		=>	time(),
				'params'	=>	json_encode($params),
				'values'	=>	json_encode($values),
				'ip'		=>	$this->request->ip(),
				'func'		=>	$this->request->action(),
				'cla'		=>	$this->request->controller()
			);
			model('Homelog')->insert($homelog);
		}
    }


	public function getDownloadUrl(){
    	$setting = model('Setting')->field('android,iphone')->where('id','>',0)->findOrEmpty();
    	if (!$setting) return json(['code'=>0,'code_dec'=>'无法获取下载地址！']);

		$data['android'] = $setting['android'];
		$data['iphone']  = $setting['iphone'];

    	return json(['code'=>1,'data'=>$data]);
    }

}
